////////////////////////////////////////////////////////////////////////////
//
//  Crytek Engine Source File.
//  Copyright (C), Crytek Studios, 2001-2010.
// -------------------------------------------------------------------------
//  File name:   ThreadInfo.cpp
//  Version:     v1.00
//  Created:     23/6/2012 by Paul Reindell.
//  Description: 
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "ThreadInfo.h"

#if defined(ENABLE_PROFILING_CODE)

////////////////////////////////////////////////////////////////////////////

// THIS IS EVIL HACK to get the ThreadEnum functions on gringo; might not work in further releases!!














































////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
#if defined(WIN32) || defined(WIN64) || defined(gringo)
////////////////////////////////////////////////////////////////////////////
void SThreadInfo::GetCurrentThreads(TThreadInfo& threadsOut)
{
	HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
	DWORD currProcessId = GetCurrentProcessId();
	if (h != INVALID_HANDLE_VALUE)
	{
		THREADENTRY32 te;
		te.dwSize = sizeof(te);
		if (Thread32First(h, &te))
		{
			do
			{
				if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID))
				{
					if (te.th32OwnerProcessID==currProcessId)
					{
						threadsOut[te.th32ThreadID] = CryThreadGetName(te.th32ThreadID);
					}
				}
				te.dwSize = sizeof(te);
			} while (Thread32Next(h, &te));
		}
		CloseHandle(h);
	}
}

////////////////////////////////////////////////////////////////////////////
void SThreadInfo::OpenThreadHandles(TThreads& threadsOut, const TThreadIds& threadIds/* = TThreadIds()*/, bool ignoreCurrThread/* = true*/)
{
	TThreadIds threadids = threadIds;
	if (threadids.empty())
	{
		TThreadInfo threads;
		GetCurrentThreads(threads);
		DWORD currThreadId = GetCurrentThreadId();
		for (TThreadInfo::iterator it = threads.begin(), end = threads.end(); it != end; ++it)
			if (!ignoreCurrThread || it->first != currThreadId)
				threadids.push_back(it->first);
	}
	for (TThreadIds::iterator it = threadids.begin(), end = threadids.end(); it != end; ++it)
	{
		SThreadHandle thread;
		thread.Id = *it;
		thread.Handle = OpenThread(THREAD_ALL_ACCESS, FALSE, *it);
		threadsOut.push_back(thread);
	}
}

////////////////////////////////////////////////////////////////////////////
void SThreadInfo::CloseThreadHandles(const TThreads& threads)
{
	for (TThreads::const_iterator it = threads.begin(), end = threads.end(); it != end; ++it)
		CloseHandle(it->Handle);
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////


















































































#elif defined(LINUX) || defined(CAFE)
void SThreadInfo::GetCurrentThreads(TThreadInfo& threadsOut)
{
	assert(false); // not implemented!
}

////////////////////////////////////////////////////////////////////////////
void SThreadInfo::OpenThreadHandles(TThreads& threadsOut, const TThreadIds& threadIds/* = TThreadIds()*/, bool ignoreCurrThread/* = true*/)
{
	assert(false); // not implemented!
}

////////////////////////////////////////////////////////////////////////////
void SThreadInfo::CloseThreadHandles(const TThreads& threads)
{
	assert(false); // not implemented!
}

#endif //#if defined(WIN32) || defined(WIN64) || defined(gringo)
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

#endif //#if defined(ENABLE_PROFILING_CODE)
